package com.fs.service;

import com.fs.mapper.DistrictMapper;
import com.fs.pojo.District;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.List;

@Slf4j
@Component
@Order(Ordered.HIGHEST_PRECEDENCE+1)
public class RedisCommandLineRunnerImpl implements CommandLineRunner {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private DistrictMapper districtMapper;

    @Override
    public void run(String... args) throws Exception {

        log.info("项目启动后，初始化地区信息进redis");
        ListOperations<String,District> lo = redisTemplate.opsForList();
        List<District> list = lo.range("key-0", 0, -1);
        if(list==null || list.size()<1){
            log.info("==读取地区设置进入redis 开始 等待加载==");
            List<District> districtList = districtMapper.selectList(null);
            for (District d : districtList) {
                lo.rightPush("key-"+d.getParentId(),d);
            }
            log.info("==读取地区设置进入redis 结束==");
        }
    }
}
